package testng.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.citybrain.flow.platform.manager.FlowEngineApplication;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.JsonUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ModelEditVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;

/**
 * @author cheng.huaxing
 * @date 2019-03-13
 */
@SpringBootTest(classes = FlowEngineApplication.class)
public class FlowModelControllerTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @BeforeClass
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }


    //@Test(dataProvider = "modelUpdateJson")
    public void testUpdateModel(String modelUpdateJson) throws Exception {
        ModelEditVo modelEditVo = JsonUtils.toObject(modelUpdateJson, ModelEditVo.class);
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/model/save").characterEncoding("UTF-8")
                .content(JsonUtils.toJson(modelEditVo)).contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testModelDetail() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/model/detail").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"modelId\":435076\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testModelDeploy() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/model/deploy").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"modelId\":435076\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testModelDeploymentDetail() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/model/deployment/detail").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"deploymentId\":435003\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testModelDeploymentList() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/model/deployment/list").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"appId\":2,\n" +
                        "  \"pageNum\":3,\n" +
                        "  \"pageSize\":10,\n" +
                        "  \"timeStart\":\"2019-01-22\",\n" +
                        "  \"timeEnd\":\"2019-03-27\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testModelDelete() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/model/delete").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"modelId\":435076\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    @DataProvider(name = "modelUpdateJson")
    public Object[][] provideModelUpdateJson() {
        return new Object[][] {{"{\n" +
                "    \"name\":\"API发布测试3\",\n" +
                "    \"description\":\"API发布\",\n" +
                "    \"key\":\"API_DEPLOY\",\n" +
                "    \"modelId\":\"" + 435076 + "\",\n" +
                "    \"modelJson\":{\n" +
                "        \"modelId\":\"" + 435076 + "\",\n" +
                "        \"properties\":{\n" +
                "            \"process_id\":\"API_DEPLOY\",\n" +
                "            \"name\":\"API发布测试3\",\n" +
                "            \"documentation\":\"API发布\",\n" +
                "            \"process_author\":\"\",\n" +
                "            \"process_version\":\"\",\n" +
                "            \"process_namespace\":\"\",\n" +
                "            \"process_historylevel\":\"\",\n" +
                "            \"isexecutable\":true,\n" +
                "            \"dataproperties\":\"\",\n" +
                "            \"executionlisteners\":\"\",\n" +
                "            \"eventlisteners\":\"\",\n" +
                "            \"signaldefinitions\":\"\",\n" +
                "            \"messagedefinitions\":\"\",\n" +
                "            \"process_potentialstarteruser\":\"\",\n" +
                "            \"process_potentialstartergroup\":\"\",\n" +
                "            \"iseagerexecutionfetch\":\"false\"\n" +
                "        },\n" +
                "        \"nodes\":[\n" +
                "            {\n" +
                "                \"model\":{\n" +
                "                    \"resourceId\":\"id0f99d2a7\",\n" +
                "                    \"properties\":{\n" +
                "                        \"formproperties\":\"\",\n" +
                "                        \"multiinstance_collection\":\"\",\n" +
                "                        \"formkeydefinition\":\"\",\n" +
                "                        \"tasklisteners\":\"\",\n" +
                "                        \"multiinstance_condition\":\"\",\n" +
                "                        \"executionlisteners\":\"\",\n" +
                "                        \"documentation\":\"\",\n" +
                "                        \"prioritydefinition\":\"\",\n" +
                "                        \"categorydefinition\":\"\",\n" +
                "                        \"multiinstance_cardinality\":\"\",\n" +
                "                        \"multiinstance_variable\":\"\",\n" +
                "                        \"usertaskassignment\":\"13580447986\",\n" +
                "                        \"multiinstance_type\":\"None\",\n" +
                "                        \"exclusivedefinition\":\"false\",\n" +
                "                        \"formreference\":\"\",\n" +
                "                        \"duedatedefinition\":\"\",\n" +
                "                        \"asynchronousdefinition\":\"false\",\n" +
                "                        \"candidateGroups\":[\n" +
                "\n" +
                "                        ],\n" +
                "                        \"candidateUsers\":[\n" +
                "                            {\n" +
                "                                \"name\":\"成华星\",\n" +
                "                                \"code\":\"13580447986\"\n" +
                "                            }\n" +
                "                        ],\n" +
                "                        \"overrideid\":\"API_DEPLOY_\",\n" +
                "                        \"name\":\"API发布申请\",\n" +
                "                        \"isforcompensation\":\"false\",\n" +
                "                        \"skipexpression\":\"\"\n" +
                "                    },\n" +
                "                    \"stencil\":{\n" +
                "                        \"id\":\"UserTask\"\n" +
                "                    }\n" +
                "                },\n" +
                "                \"id\":\"id0f99d2a7\",\n" +
                "                \"index\":0,\n" +
                "                \"label\":\"API发布申请\",\n" +
                "                \"shape\":\"UserTask\",\n" +
                "                \"size\":\"120*60\",\n" +
                "                \"type\":\"node\",\n" +
                "                \"x\":252,\n" +
                "                \"y\":206,\n" +
                "                \"style\":{\n" +
                "                    \"fill\":\"#fff\",\n" +
                "                    \"stroke\":\"#FFBB95\"\n" +
                "                },\n" +
                "                \"color\":\"#FFBB95\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"model\":{\n" +
                "                    \"resourceId\":\"id12f3b672\",\n" +
                "                    \"properties\":{\n" +
                "                        \"overrideid\":\"\",\n" +
                "                        \"name\":\"开始\",\n" +
                "                        \"documentation\":\"\",\n" +
                "                        \"executionlisteners\":\"\",\n" +
                "                        \"initiator\":\"\",\n" +
                "                        \"formkeydefinition\":\"\",\n" +
                "                        \"formreference\":\"\",\n" +
                "                        \"formproperties\":{\n" +
                "\n" +
                "                        }\n" +
                "                    },\n" +
                "                    \"stencil\":{\n" +
                "                        \"id\":\"StartNoneEvent\"\n" +
                "                    }\n" +
                "                },\n" +
                "                \"id\":\"id12f3b672\",\n" +
                "                \"index\":2,\n" +
                "                \"label\":\"开始\",\n" +
                "                \"shape\":\"StartNoneEvent\",\n" +
                "                \"size\":\"32*32\",\n" +
                "                \"type\":\"node\",\n" +
                "                \"x\":112,\n" +
                "                \"y\":206,\n" +
                "                \"style\":{\n" +
                "                    \"fill\":\"#fff\",\n" +
                "                    \"stroke\":\"#a5e181\"\n" +
                "                },\n" +
                "                \"color\":\"#a5e181\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"model\":{\n" +
                "                    \"resourceId\":\"idbd27fd9f\",\n" +
                "                    \"properties\":{\n" +
                "                        \"formproperties\":\"\",\n" +
                "                        \"multiinstance_collection\":\"\",\n" +
                "                        \"formkeydefinition\":\"\",\n" +
                "                        \"tasklisteners\":\"\",\n" +
                "                        \"multiinstance_condition\":\"\",\n" +
                "                        \"executionlisteners\":\"\",\n" +
                "                        \"documentation\":\"\",\n" +
                "                        \"prioritydefinition\":\"\",\n" +
                "                        \"categorydefinition\":\"\",\n" +
                "                        \"multiinstance_cardinality\":\"\",\n" +
                "                        \"multiinstance_variable\":\"\",\n" +
                "                        \"usertaskassignment\":\"15577903540\",\n" +
                "                        \"multiinstance_type\":\"None\",\n" +
                "                        \"duedate\":{\n" +
                "                            \"relativeTime\":\"start\",\n" +
                "                            \"day\":1,\n" +
                "                            \"hour\":2,\n" +
                "                            \"minute\":3,\n" +
                "                            \"second\":4\n" +
                "                        },\n" +
                "                        \"exclusivedefinition\":\"false\",\n" +
                "                        \"formreference\":\"\",\n" +
                "                        \"duedatedefinition\":\"\",\n" +
                "                        \"asynchronousdefinition\":\"false\",\n" +
                "                        \"candidateGroups\":[\n" +
                "\n" +
                "                        ],\n" +
                "                        \"candidateUsers\":[\n" +
                "                            {\n" +
                "                                \"name\":\"李良宏\",\n" +
                "                                \"code\":\"15577903540\"\n" +
                "                            }\n" +
                "                        ],\n" +
                "                        \"overrideid\":\"API_DEPLOY_OK\",\n" +
                "                        \"name\":\"API发布审核\",\n" +
                "                        \"isforcompensation\":\"false\",\n" +
                "                        \"skipexpression\":\"\"\n" +
                "                    },\n" +
                "                    \"stencil\":{\n" +
                "                        \"id\":\"UserTask\"\n" +
                "                    }\n" +
                "                },\n" +
                "                \"id\":\"idbd27fd9f\",\n" +
                "                \"index\":4,\n" +
                "                \"label\":\"API发布审核\",\n" +
                "                \"shape\":\"UserTask\",\n" +
                "                \"size\":\"120*60\",\n" +
                "                \"type\":\"node\",\n" +
                "                \"x\":437,\n" +
                "                \"y\":206,\n" +
                "                \"style\":{\n" +
                "                    \"fill\":\"#fff\",\n" +
                "                    \"stroke\":\"#FFBB95\"\n" +
                "                },\n" +
                "                \"color\":\"#FFBB95\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"model\":{\n" +
                "                    \"resourceId\":\"id44ff391c\",\n" +
                "                    \"properties\":{\n" +
                "                        \"overrideid\":\"\",\n" +
                "                        \"name\":\"\",\n" +
                "                        \"documentation\":\"\",\n" +
                "                        \"executionlisteners\":\"\"\n" +
                "                    },\n" +
                "                    \"stencil\":{\n" +
                "                        \"id\":\"EndNoneEvent\"\n" +
                "                    }\n" +
                "                },\n" +
                "                \"id\":\"id44ff391c\",\n" +
                "                \"index\":5,\n" +
                "                \"label\":\"结束\",\n" +
                "                \"shape\":\"EndNoneEvent\",\n" +
                "                \"size\":\"32*32\",\n" +
                "                \"type\":\"node\",\n" +
                "                \"x\":607,\n" +
                "                \"y\":206,\n" +
                "                \"style\":{\n" +
                "                    \"fill\":\"#fff\",\n" +
                "                    \"stroke\":\"#f10000\"\n" +
                "                },\n" +
                "                \"color\":\"#fbeeee\"\n" +
                "            }\n" +
                "        ],\n" +
                "        \"edges\":[\n" +
                "            {\n" +
                "                \"model\":{\n" +
                "                    \"resourceId\":\"id6a3cf4de\",\n" +
                "                    \"properties\":{\n" +
                "                        \"overrideid\":\"\",\n" +
                "                        \"name\":\"\",\n" +
                "                        \"documentation\":\"\",\n" +
                "                        \"conditionsequenceflow\":\"\",\n" +
                "                        \"executionlisteners\":\"\",\n" +
                "                        \"defaultflow\":\"false\",\n" +
                "                        \"skipexpression\":\"\"\n" +
                "                    },\n" +
                "                    \"stencil\":{\n" +
                "                        \"id\":\"SequenceFlow\"\n" +
                "                    }\n" +
                "                },\n" +
                "                \"id\":\"id6a3cf4de\",\n" +
                "                \"index\":1,\n" +
                "                \"source\":\"id12f3b672\",\n" +
                "                \"sourceAnchor\":3,\n" +
                "                \"target\":\"id0f99d2a7\",\n" +
                "                \"targetAnchor\":2,\n" +
                "                \"style\":{\n" +
                "\n" +
                "                },\n" +
                "                \"shape\":\"flow-polyline-round\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"model\":{\n" +
                "                    \"resourceId\":\"id3b1b5191\",\n" +
                "                    \"properties\":{\n" +
                "                        \"overrideid\":\"\",\n" +
                "                        \"name\":\"\",\n" +
                "                        \"documentation\":\"\",\n" +
                "                        \"conditionsequenceflow\":\"\",\n" +
                "                        \"executionlisteners\":\"\",\n" +
                "                        \"defaultflow\":\"false\",\n" +
                "                        \"skipexpression\":\"\"\n" +
                "                    },\n" +
                "                    \"stencil\":{\n" +
                "                        \"id\":\"SequenceFlow\"\n" +
                "                    }\n" +
                "                },\n" +
                "                \"id\":\"id3b1b5191\",\n" +
                "                \"index\":3,\n" +
                "                \"source\":\"id0f99d2a7\",\n" +
                "                \"sourceAnchor\":3,\n" +
                "                \"target\":\"idbd27fd9f\",\n" +
                "                \"targetAnchor\":2,\n" +
                "                \"style\":{\n" +
                "\n" +
                "                },\n" +
                "                \"shape\":\"flow-polyline-round\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"model\":{\n" +
                "                    \"resourceId\":\"id7ac60821\",\n" +
                "                    \"properties\":{\n" +
                "                        \"overrideid\":\"\",\n" +
                "                        \"name\":\"\",\n" +
                "                        \"documentation\":\"\",\n" +
                "                        \"conditionsequenceflow\":\"\",\n" +
                "                        \"executionlisteners\":\"\",\n" +
                "                        \"defaultflow\":\"false\",\n" +
                "                        \"skipexpression\":\"\"\n" +
                "                    },\n" +
                "                    \"stencil\":{\n" +
                "                        \"id\":\"SequenceFlow\"\n" +
                "                    }\n" +
                "                },\n" +
                "                \"id\":\"id7ac60821\",\n" +
                "                \"index\":6,\n" +
                "                \"source\":\"idbd27fd9f\",\n" +
                "                \"sourceAnchor\":3,\n" +
                "                \"target\":\"id44ff391c\",\n" +
                "                \"targetAnchor\":2,\n" +
                "                \"style\":{\n" +
                "\n" +
                "                },\n" +
                "                \"shape\":\"flow-polyline-round\"\n" +
                "            }\n" +
                "        ],\n" +
                "        \"stencil\":{\n" +
                "            \"id\":\"BPMNDiagram\"\n" +
                "        },\n" +
                "        \"stencilset\":{\n" +
                "            \"namespace\":\"http://b3mn.org/stencilset/bpmn2.0#\",\n" +
                "            \"url\":\"../editor/stencilsets/bpmn2.0/bpmn2.0.json\"\n" +
                "        }\n" +
                "    },\n" +
                "    \"appId\":1,\n" +
                "    \"busiId\":1\n" +
                "}"}};
    }
}